/*
 * Copyright Aristool AG Switzerland
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package org.opprua.tidy.gens;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import org.opprua.tidy.util.CodePrint;

/**
 * TODO: add Java doc
 * 
 * Prepared for first commit: 15.04.2007
 * 
 * @author Lin Li
 */
public abstract class Gtor {

  private List<String> comments = new ArrayList<String>();
  private List<String> annotations = new ArrayList<String>();

  protected Gtor() {
  }

  /**
   */
  public Gtor comment(String c) {
    StringTokenizer st = new StringTokenizer(c, "\n");
    while (st.hasMoreTokens()) {
      comments.add(st.nextToken());
    }
    return this;
  }

  public String clearComment() {
    StringBuffer b = new StringBuffer();
    for (int i = 0; i < comments.size(); i++) {
      b.append(comments.get(i)).append("\n");
    }
    return b.toString();
  }

  /**
   */
  public Gtor blockComments(CodePrint w) {
    for (Iterator<String> i = comments.iterator(); i.hasNext();) {
      String s = i.next();
      w.println("// " + s);
    }
    return this;
  }

  /**
   */
  public Gtor annotate(String c) {
    annotations.add(c);
    return this;
  }

  /**
   */
  public Gtor annotate(List<String> c) {
    for (int i = 0; i < c.size(); i++) {
      annotate(c.get(i));
    }
    return this;
  }

  /**
   */
  public Gtor printAnnotation(CodePrint w) {
    for (Iterator<String> i = annotations.iterator(); i.hasNext();) {
      String s = i.next();
      if (!s.trim().startsWith("@")) s = "@" + s;
      w.println(s);
    }
    return this;
  }

  /**
   */
  public Gtor javadoc(CodePrint w) {
    if (comments.size() == 0) {
      return this;
    }

    w.println("/**");
    for (Iterator<String> i = comments.iterator(); i.hasNext();) {
      String s = (String) i.next();
      w.println(" * " + s);
    }
    w.println(" */");
    return this;
  }

  /**
   */
  public boolean test(String string) {
    return string != null && string.trim().length() > 0;
  }

  /**
   */
  abstract public void dump(CodePrint w);
}
